package Athena::Engine::Phys;

use strict;
use vars qw(@ISA);

use Athena::Connection;
use Athena::Engine;
use Exporter;

@ISA=qw(Athena::Engine);

sub new {
    # Get the class name
  my $stInvocant = shift(@_);
  my $stClass = ref($stInvocant) || $stInvocant;

  return $stClass->SUPER::new(@_);
}

sub refresh {
  my $pSelf = shift(@_);
  my $cConn = $pSelf->{connection};
  my $stSerials = $cConn->httpGet($cConn->getUrl() . '/status/phys/?h=*,f=%s');
  my @arSerials = split(/\n/, $stSerials);
  my $stSerial;
  foreach $stSerial (@arSerials) {
    $pSelf->_deserialize($stSerial);
  }
}

sub update {
  my $pSelf = shift(@_);
  my $cConn = $pSelf->{connection};
  my $pPhys;
  my $stUrl;
  my $stResult;
  foreach $pPhys (keys(%{$pSelf->{table}})) {
    $stUrl = $cConn->getUrl()
      . '/update/phys/?'
	. "h=" . $pSelf->{table}->{$pPhys}->{host}
	  . ",o=" . $pSelf->{table}->{$pPhys}->{on}
	    . ",u=" . $pSelf->{table}->{$pPhys}->{up}
	      . ",i=" . $pSelf->{table}->{$pPhys}->{in};
    $stResult = $cConn->httpGet($stUrl);
    if (($stResult) && ($stResult ne "\n")) {
      print STDERR "athena.pl: error updating phys: $stUrl\n";
    }
  }
}

sub _deserialize {
  my $pSelf = shift(@_);
  my $stSerial = shift(@_);
  my ($idx, $exists, $host, $on, $up, $in, $mtime, $dtime, $cpu, $net, $ld,
      $mem, $disk, $cus0, $cus1, $cus2, $cus3, $cus4, $cus5, $cus6, $cus7,
      $cus8, $cus9, $farm_member_list) = split(/\|/, $stSerial);
  $pSelf->{table}->{$idx}->{exists} = $exists;
  $pSelf->{table}->{$idx}->{host} = $host;
  $pSelf->{table}->{$idx}->{on} = $on;
  $pSelf->{table}->{$idx}->{up} = $up;
  $pSelf->{table}->{$idx}->{in} = $in;
  $pSelf->{table}->{$idx}->{mtime} = $mtime;
  $pSelf->{table}->{$idx}->{dtime} = $dtime;
  $pSelf->{table}->{$idx}->{cpu} = $cpu;
  $pSelf->{table}->{$idx}->{net} = $net;
  $pSelf->{table}->{$idx}->{ld} = $ld;
  $pSelf->{table}->{$idx}->{mem} = $mem;
  $pSelf->{table}->{$idx}->{disk} = $disk;
  $pSelf->{table}->{$idx}->{cus0} = $cus0;
  $pSelf->{table}->{$idx}->{cus1} = $cus1;
  $pSelf->{table}->{$idx}->{cus2} = $cus2;
  $pSelf->{table}->{$idx}->{cus3} = $cus3;
  $pSelf->{table}->{$idx}->{cus4} = $cus4;
  $pSelf->{table}->{$idx}->{cus5} = $cus5;
  $pSelf->{table}->{$idx}->{cus6} = $cus6;
  $pSelf->{table}->{$idx}->{cus7} = $cus7;
  $pSelf->{table}->{$idx}->{cus8} = $cus8;
  $pSelf->{table}->{$idx}->{cus9} = $cus9;
  $pSelf->{table}->{$idx}->{farms} = $farm_member_list;
  # create index by host name for display
  $pSelf->{idx_name}->{$host} = $idx;
  #  my @farm_membership = split(",", $farm_member_list);
  #  my $key;
  #  foreach $key (@farm_membership) {
  #    $pSelf->{table}->{$idx}->{membership_farm_idx}->{$key} = 1;
  #  }
  return $idx;
}

1;
